package com.ziytek.web.citizen.util;

import com.ziytek.web.citizen.constant.NumConstant;
import com.ziytek.web.citizen.model.tcp.req.TcpBatteryDetailReq;
import com.ziytek.web.citizen.model.tcp.req.TcpRealTimeReq;
import lombok.extern.slf4j.Slf4j;

import java.nio.ByteBuffer;

/**
 * @author fengh
 */
@Slf4j
public class ClientDataUtils {

    public static TcpRealTimeReq realTimeReq(byte[] data) {
        ByteBuffer byteBuffer = ByteBuffer.wrap(data);

        var topic1 = byteBuffer.get() & 0xFF;
        var topicOne = HexUtil.fillZero(Integer.toHexString(topic1), NumConstant.INT_TWO).toUpperCase();
        var topic2 = byteBuffer.get() & 0xFF;
        var topicTwo = HexUtil.fillZero(Integer.toHexString(topic2), NumConstant.INT_TWO).toUpperCase();

        var cabinetNo = Integer.toHexString(byteBuffer.get() & 0xFF);
        var door = Integer.toHexString(byteBuffer.get() & 0xFF);

        TcpRealTimeReq req = new TcpRealTimeReq();
        req.setTopic(topicOne + topicTwo);
        req.setCabinetNo(cabinetNo);
        req.setDoor(door);
//        req.setMessageEnd("CC0D0A");

        return req;
    }


    public static TcpBatteryDetailReq batteryDetailReq(byte[] data) {
        ByteBuffer byteBuffer = ByteBuffer.wrap(data);

        var topic1 = byteBuffer.get() & 0xFF;
        var topicOne = HexUtil.fillZero(Integer.toHexString(topic1), NumConstant.INT_TWO).toUpperCase();
        var topic2 = byteBuffer.get() & 0xFF;
        var topicTwo = HexUtil.fillZero(Integer.toHexString(topic2), NumConstant.INT_TWO).toUpperCase();

        var cabinetNo = Integer.toHexString(byteBuffer.get() & 0xFF);
        var door = Integer.toHexString(byteBuffer.get() & 0xFF);
        var lineNo = Integer.toHexString(byteBuffer.get() & 0xFF);
        var channelNo = Integer.toHexString(byteBuffer.get() & 0xFF);

        var time1 = byteBuffer.get() & 0xFF;
        var timeOne = HexUtil.fillZero(Integer.toHexString(time1), NumConstant.INT_TWO).toUpperCase();
        var time2 = byteBuffer.get() & 0xFF;
        var timeTwo = HexUtil.fillZero(Integer.toHexString(time2), NumConstant.INT_TWO).toUpperCase();

        TcpBatteryDetailReq req = new TcpBatteryDetailReq();
        req.setTopic(topicOne + topicTwo);
        req.setCabinetNo(cabinetNo);
        req.setDoor(door);
        req.setLineNo(Integer.parseInt(lineNo));
        req.setChannelNo(Integer.parseInt(channelNo));
        //req.setMessageEnd("CC0D0A");
        var time = timeOne + timeTwo;
        req.setTimeMinute(Long.parseLong(time));

        return req;
    }


}
